﻿// 功能：INI文件读写API
// 描述：
// 作者：温伟鹏
// 日期：2010/5/12 11:15:50

using System;
using System.Runtime.InteropServices;

namespace MeizuSDK.Core
{
    /// <summary>
    /// INI文件读写API
    /// </summary>
    public sealed class ReadWriteIniAPI
    {
        /// <summary>
        /// Ini文件的创建,Ini文件的字符编码采用Unicode little编码，即0xFF, 0xFE开头，和txt一样
        /// </summary>
        /// <param name="iniFileName">Ini文件文件名</param>
        /// <returns></returns>
        [DllImport("PlatformAPIWrapper.dll", SetLastError = true, EntryPoint = "IniCreateFileEx")]
        public static extern bool IniCreateFile(string iniFileName);

        /// <summary>
        /// 判断某个段是否存在
        /// </summary>
        /// <param name="pAppName">段名</param>
        /// <param name="pFileName"> .ini文件名</param>
        /// <returns>存在返回true,不存在返回false</returns>
        [DllImport("PlatformAPIWrapper.dll", SetLastError = true, EntryPoint = "IsExistAppEx")]
        public static extern bool IsExistApp(string pAppName, string pFileName);

        /// <summary>
        /// 往ini文件中加入一个字符串的值
        /// </summary>
        /// <param name="pAppName">段名</param>
        /// <param name="pKeyName">键名</param>
        /// <param name="pString">键值</param>
        /// <param name="pFileName">.ini文件名</param>
        /// <remarks>
        /// 当pKeyName == NULL时，会删除整个pAppName段，当pString == NULL时，会删除pAppName段下的pKeyName键.
        /// </remarks>
        /// <returns>操作成功，返回true；失败返回false</returns>
        [DllImport("PlatformAPIWrapper.dll", SetLastError = true, EntryPoint = "IniWriteStringEx")]
        public static extern bool IniWriteString(string pAppName, string pKeyName, string pString, string pFileName);

        /// <summary>
        /// 从ini文件中读取一个字符串值
        /// </summary>
        /// <param name="pAppName">段名</param>
        /// <param name="pKeyName">键名</param>
        /// <param name="pString">读到的键值</param>
        /// <param name="pFileName">.ini文件名</param>
        /// <returns>操作成功，返回true；失败返回false</returns>
        [DllImport("PlatformAPIWrapper.dll", SetLastError = true, EntryPoint = "IniReadStringEx")]
        public static extern bool IniReadString(string pAppName, string pKeyName, ref string pString, string pFileName);

        /// <summary>
        /// 往ini文件中加入一个整形值
        /// </summary>
        /// <param name="pAppName">段名</param>
        /// <param name="pKeyName">键名</param>
        /// <param name="dwValue">键值（整形）</param>
        /// <param name="pFileName">.ini文件名</param>
        /// <returns></returns>
        [DllImport("PlatformAPIWrapper.dll", SetLastError = true, EntryPoint = "IniWriteIntEx")]
        public static extern bool IniWriteInt(string pAppName, string pKeyName, int dwValue, string pFileName);

        /// <summary>
        /// 从ini文件中读到一个整形值
        /// </summary>
        /// <param name="pAppName">段名</param>
        /// <param name="pKeyName">键名</param>
        /// <param name="dwValue">读到的键值</param>
        /// <param name="pFileName">.ini文件名</param>
        /// <returns></returns>
        [DllImport("PlatformAPIWrapper.dll", SetLastError = true, EntryPoint = "IniReadIntEx")]
        public static extern bool IniReadInt(string pAppName, string pKeyName, ref int dwValue, string pFileName);
    }
}
